package com.smart.campus.web.controller.school;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.smart.campus.framework.util.ShiroUtils;
import com.smart.campus.school.domain.PsychologicalTestItem;
import com.smart.campus.school.service.IPsychologicalTestItemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.smart.campus.common.annotation.Log;
import com.smart.campus.common.enums.BusinessType;
import com.smart.campus.school.domain.PsychologicalTest;
import com.smart.campus.school.service.IPsychologicalTestService;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.utils.poi.ExcelUtil;
import com.smart.campus.common.core.page.TableDataInfo;

/**
 * 心理测试Controller
 * 
 * @author Zhihe.Li
 * @date 2020-05-27
 */
@Controller
@RequestMapping("/school/psychologicaTest")
public class PsychologicalTestController extends BaseController
{
    private String prefix = "school/psychologicaTest";

    @Autowired
    private IPsychologicalTestService psychologicalTestService;
    @Autowired
    private IPsychologicalTestItemService psychologicalTestItemService;

    @RequiresPermissions("school:psychologicaTest:view")
    @GetMapping()
    public String psychologicaTest()
    {
        return prefix + "/psychologicaTest";
    }

    /**
     * 查询心理测试列表
     */
    @RequiresPermissions("school:psychologicaTest:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PsychologicalTest psychologicalTest)
    {
        startPage();
        List<PsychologicalTest> list = psychologicalTestService.selectPsychologicalTestList(psychologicalTest);
        return getDataTable(list);
    }

    /**
     * 导出心理测试列表
     */
    @RequiresPermissions("school:psychologicaTest:export")
    @Log(title = "心理测试", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PsychologicalTest psychologicalTest)
    {
        List<PsychologicalTest> list = psychologicalTestService.selectPsychologicalTestList(psychologicalTest);
        ExcelUtil<PsychologicalTest> util = new ExcelUtil<PsychologicalTest>(PsychologicalTest.class);
        return util.exportExcel(list, "psychologicaTest");
    }

    /**
     * 新增心理测试
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存心理测试
     */
    @RequiresPermissions("school:psychologicaTest:add")
    @Log(title = "心理测试", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PsychologicalTest psychologicalTest)
    {
        return toAjax(psychologicalTestService.insertPsychologicalTest(psychologicalTest));
    }

    /**
     * 修改心理测试
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        PsychologicalTest psychologicalTest = psychologicalTestService.selectPsychologicalTestById(id);
        mmap.put("psychologicalTest", psychologicalTest);
        return prefix + "/edit";
    }

    /**
     * 修改保存心理测试
     */
    @RequiresPermissions("school:psychologicaTest:edit")
    @Log(title = "心理测试", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PsychologicalTest psychologicalTest)
    {
        return toAjax(psychologicalTestService.updatePsychologicalTest(psychologicalTest));
    }

    /**
     * 删除心理测试
     */
    @RequiresPermissions("school:psychologicaTest:remove")
    @Log(title = "心理测试", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(psychologicalTestService.deletePsychologicalTestByIds(ids));
    }


    /**
     * 答题
     */
    @GetMapping("/answer/{id}")
    public String answer(@PathVariable("id") Long id, ModelMap mmap) {
        PsychologicalTest psychologicalTest = psychologicalTestService.selectPsychologicalTestById(id);
        PsychologicalTestItem psychologicalTestItem = new PsychologicalTestItem();
        psychologicalTestItem.setTestId(id);
        List<PsychologicalTestItem> itemList = psychologicalTestItemService.selectPsychologicalTestItemList(psychologicalTestItem);
        for (PsychologicalTestItem testItem : itemList) {
            String answer = testItem.getAnswer();
            testItem.setAnswerArray(JSON.parseArray(answer));
        }
        mmap.put("psychologicalTest", psychologicalTest);
        mmap.put("itemList", itemList);
        mmap.put("user", ShiroUtils.getSysUser());
        return prefix + "/answer";
    }
}
